package com.eke.action.course;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause.Entry;
import com.eke.bean.Course;
import com.eke.bean.CourseType;
import com.eke.bean.Sign_up;
import com.eke.bean.Users;
import com.eke.service.CourseSevice;
import com.eke.service.UsersService;
import com.jason.mvc.annotations.Ajax;
import com.jason.mvc.annotations.Namespace;
import com.jason.mvc.annotations.RequestMapping;
import com.jason.mvc.utils.ApplicationContext;

@Namespace("/course")
@Component("courseAction")
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class CourseAction {
	@Resource
	private CourseSevice courseService;
	@Resource
	private UsersService usersService;
	private Course course;
	private Sign_up sign_up;
	private Users users;
	private CourseType coursetype;
	private Integer pageCount = 1;
	private Integer pageSize = 10;

	/**
	 * 推荐课程(用于首页显示)
	 * 
	 * @return {@link Map}<{@link String},{@link Object}>
	 */
	@RequestMapping("queryRecommedCourse")
	@Ajax(true)
	public Map<String, Object> queryRecommedCourse() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("course", courseService.queryRecommendCourses());
		return map;
	}

	/**
	 * 查询4个大课程分类(用于首页显示)
	 * 
	 * @return {@link Map}<{@link String},{@link Object}>
	 */
	@RequestMapping("queryFourCourseType")
	@Ajax(true)
	public Map<String, Object> queryFourCourseType() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("bigCourseType", courseService.queryFourBigCourseType());
		return map;
	}

	/**
	 * 通过课程分类id查询课程
	 * 
	 * @return {@link Map}<{@link String},{@link Object}>
	 */
	@RequestMapping("queryRecommenCourseByTypeId")
	@Ajax(true)
	public Map<String, Object> queryRecommenCourseByTypeId() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("course", courseService.queryRecommendCourseByCourseTypeId(course.getCourseTypeId()));
		return map;
	}

	/**
	 * 查询所有一级分类
	 * 
	 * @return {@link Map}<{@link String},{@link Object}>
	 */
	@RequestMapping("queryAllBigCourseType")
	@Ajax(true)
	public Map<String, Object> queryAllBigCourseType() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("courseBigId", courseService.queryAllBigCourseType());
		return map;
	}

	/**
	 * 查询所有二级分类byId
	 *
	 * @return {@link Map}<{@link String},{@link Object}>
	 */
	@RequestMapping("queryAllTwoCourseType")
	@Ajax(true)
	public Map<String, Object> queryAllTwoCourseType() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("courseTwoId", courseService.queryAllTwoCourseType(coursetype.getCourseSubTypeId()));
		return map;
	}

	/**
	 * 查询所有三级分类
	 *
	 * @return {@link Map}<{@link String},{@link Object}>
	 */
	@RequestMapping("queryAllThreeCourseType")
	@Ajax(true)
	public Map<String, Object> queryAllThreeCourseType() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("courseThreeId", courseService.queryAllThreeCourseType());
		return map;
	}

	/**
	 * 查询所有二级分类无需参数
	 *
	 * @return {@link Map}<{@link String},{@link Object}>
	 */
	@RequestMapping("queryAllTwoCourseTypeNoParms")
	@Ajax(true)
	public Map<String, Object> queryAllTwoCourseTypeNoParms() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("courseTwoId", courseService.queryAllTwoCourseType());
		return map;
	}

	/**
	 * 分页查询所有课程分类id为？的课程
	 * 
	 * @return List
	 */
	@RequestMapping("queryPagingCourseById")
	@Ajax(true)
	public Map<String, Object> queryPagingCourseById() {
		Map<String, Object> map = new HashMap<String, Object>();
		int totalRows = courseService.queryCourseTotal(course.getCourseTypeId());
		int totalPage = getTotalPage(totalRows);
		List<Course> list = courseService.queryCourseById(course.getCourseTypeId(),
				(getPageCount() - 1) * getPageSize(), getPageSize());
		map.put("totalPage", totalPage);
		map.put("pageCount", getPageCount());
		map.put("pageSize", getPageSize());
		map.put("prePage", getPageCount() - 1);
		int nextPage = getPageCount() + 1;
		map.put("nextPage", totalPage < nextPage ? 0 : nextPage);
		map.put("row", list);
		return map;
	}

	/**
	 * 分页查询所有课程分类id为？的课程
	 * 
	 * @return List
	 */
	@RequestMapping("queryAllCourse")
	@Ajax(true)
	public Map<String, Object> queryAllCourse() {
		Map<String, Object> map = new HashMap<String, Object>();
		int totalRows = courseService.queryCourseTotal();
		int totalPage = getTotalPage(totalRows);
		List<Course> list = courseService.queryAllCourse((getPageCount() - 1) * getPageSize(), getPageSize());
		map.put("totalPage", totalPage);
		map.put("pageCount", getPageCount());
		map.put("pageSize", getPageSize());
		map.put("prePage", getPageCount() - 1);
		int nextPage = getPageCount() + 1;
		map.put("nextPage", totalPage < nextPage ? 0 : nextPage);
		map.put("row", list);
		return map;
	}

	/**
	 * 查询页数
	 * 
	 * @param totalRows
	 * @return int 返回总页数
	 */
	public int getTotalPage(int totalRows) {
		int totalPage = totalRows / getPageSize();
		if (totalRows % getPageSize() > 0) {
			totalPage++;
		}
		return totalPage;
	}

	/**
	 * 查询第几页
	 * 
	 * @param totalRows
	 * @return int 返回第几页
	 */
	public Integer getPageCount() {
		return pageCount < 1 ? 1 : pageCount;
	}

	/**
	 * 查询每页数量
	 * 
	 * @param totalRows
	 * @return int 返回每页数量
	 */
	public Integer getPageSize() {
		return pageSize < 0 ? 10 : pageSize;
	}

	/**
	 * 根据课程名称查询课程
	 * 
	 * @return
	 */
	@RequestMapping("queryCourse")
	@Ajax(true)
	public Map<String, Object> queryCourse() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", courseService.queryCourseByCourseName(course.getCourseName()));
		return map;
	}

	/**
	 * 根据课程id查询课程信息
	 * 
	 * @return
	 */
	@RequestMapping("queryCourseDeatilsById")
	@Ajax(true)
	public Map<String, Object> queryCourseDeatilsById() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("course", courseService.queryCourseDeatilsById(course.getCourseId()));
		return map;
	}

	/**
	 * 添加课程
	 * 
	 * @return
	 */
	@RequestMapping("addCourse")
	@Ajax(true)
	public Map<String, Object> addCourse() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取全局变量
		javax.servlet.http.HttpSession session = ApplicationContext.getSession();
		if (session.getAttribute("users.userId") != null) {
			Integer userId = (Integer) session.getAttribute("users.userId");
			course.setTeacher_id(userId);
		}
		course.setCourseSatte(0);
		course.setCreateTime(new Timestamp(new Date().getTime()));
		map.put("result", courseService.insertCourse(course));
		return map;
	}

	/**
	 * 根据用户id查询课程
	 * 
	 */
	@RequestMapping("queryCourseByUserId")
	@Ajax(true)
	public Map<String, Object> queryCourseByUserId() {
		Map<String, Object> map = new HashMap<String, Object>();
		javax.servlet.http.HttpSession session = ApplicationContext.getSession();
		if (session.getAttribute("users.userId") != null) {
			Integer userId = (Integer) session.getAttribute("users.userId");
			int totalRows = courseService.queryCourseTotalByUserId(userId);
			int totalPage = getTotalPage(totalRows);
			List<Course> list = courseService.queryCourseByUserId(userId, (getPageCount() - 1) * getPageSize(),
					getPageSize());
			map.put("totalPage", totalPage);
			map.put("pageCount", getPageCount());
			map.put("pageSize", getPageSize());
			map.put("prePage", getPageCount() - 1);
			int nextPage = getPageCount() + 1;
			map.put("nextPage", totalPage < nextPage ? 0 : nextPage);
			map.put("row", list);
		}
		return map;
	}

	/**
	 * 查询所有课程
	 * 
	 * @return
	 */
	@RequestMapping("queryAllCourseFy")
	@Ajax(true)
	public Map<String, Object> queryAllCourseFy() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Course> list = courseService.queryAllCourse((getPageCount() - 1) * getPageSize(), getPageSize());
		map.put("code", 0);
		map.put("msg", "success");
		map.put("count", list.size());
		map.put("data", list);
		return map;
	}

	/**
	 * 用户报名
	 * 
	 * @return
	 */
	@RequestMapping("signCourse")
	@Ajax(true)
	public Map<String, Object> signCourse() {
		Map<String, Object> map = new HashMap<String, Object>();
		javax.servlet.http.HttpSession session = ApplicationContext.getSession();
		if (session.getAttribute("users.userId") != null) {
			Integer userId = (Integer) session.getAttribute("users.userId");
			if (courseService.isSignUp(userId, course.getCourseId())) {
				map.put("result", "已报名，无需重复报名");
			} else if (!usersService.isHaveEnoughIntegral(course.getCourseIntergal(), userId)) {
				map.put("result", "积分不足");
			} else {
				map.put("result", courseService.signCourse(sign_up));
			}
		} else {
			map.put("result", "还未登陆!!");
		}

		return map;
	}

	/**
	 * 用户报名
	 * 
	 * @return
	 */
	@RequestMapping("sign")
	@Ajax(true)
	public Map<String, Object> sign() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sult", courseService.signCourse(sign_up));
		return map;
	}

	/**
	 * 报名课程扣除用户积分
	 * 
	 * @return {@link Map}<{@link String},{@link Object}>
	 */
	@RequestMapping("reduceUserInteral")
	@Ajax(true)
	public Map<String, Object> reduceUserInteral() {
		int Interal = course.getCourseIntergal();
		Map<String, Object> map = new HashMap<String, Object>();
		javax.servlet.http.HttpSession session = ApplicationContext.getSession();
		if (session.getAttribute("users.userId") != null) {
			Integer userId = (Integer) session.getAttribute("users.userId");
			map.put("result", usersService.addQuestionUpdateIntegral(Interal, userId));
		}else{
			map.put("result", "扣除积分失败！");
		}
		return map;
	}
	
	/**
	 * 判断用户是否已报名
	 * 
	 * @return 
	 */
	@RequestMapping("isCanSignUp")
	@Ajax(true)
	public Map<String, Object> isCanSignUp() {
		Map<String, Object> map = new HashMap<String, Object>();
		javax.servlet.http.HttpSession session = ApplicationContext.getSession();
		if (session.getAttribute("users.userId") != null) {
			Integer userId = (Integer) session.getAttribute("users.userId");
			map.put("result", courseService.isSignUp(userId, course.getCourseId()));
			return map;
		}else{
			return null;
		}
	}
	
	
}
